package server;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;

public class RequestProcessor extends Thread {

    private Socket socket;

//    private Map<String, HttpServlet> servletMap;

    private List<Mapper.Host> hostList;

/*    public RequestProcessor(Socket socket, Map<String, HttpServlet> servletMap) {
        this.socket = socket;
        this.servletMap = servletMap;
    }*/

    public RequestProcessor(Socket socket, List<Mapper.Host> hostList) {
        this.socket = socket;
        this.hostList = hostList;
    }

    @Override
    public void run() {
        try {
            InputStream inputStream = socket.getInputStream();
            OutputStream outputStream = socket.getOutputStream();
            Request request = new Request(inputStream);
            Response response = new Response(outputStream);

            String url = request.getUrl();
            String[] split = url.split("/");
            if(split.length == 0){
                response.output(HttpProtocolUtil.getHttpHeader404());
            }else{
                String webApp = split[1];
                Mapper.Host host = null;
                for (Mapper.Host host1 : hostList) {
                    if(request.getHost().indexOf(host1.getName()) != -1){
                        host = host1;
                        break;
                    }
                }
                if(host == null){
                    response.output(HttpProtocolUtil.getHttpHeader404());
                }else{
                    Mapper.Context context = null;
                    for (Mapper.Context c : host.contexts) {
                        if(webApp.equals(c.name)){
                            context = c;
                            break;
                        }
                    }

                    if(context == null){
                        response.output(HttpProtocolUtil.getHttpHeader404());
                    }else{
                        HttpServlet httpServlet = null;
                        for (Mapper.Wrapper w : context.wrappers) {
                            if (request.getUrl().indexOf(w.url) != -1){
                                httpServlet = w.servlet;
                                break;
                            }
                        }

                        if(httpServlet == null) {
                            List<String> staticFilePath = context.staticFilePath;
                            String path = null;
                            String urlStr = request.getUrl().replaceAll("/", Matcher.quoteReplacement(File.separator)).replaceAll("\\\\", Matcher.quoteReplacement(File.separator));
                            for (String p : staticFilePath) {
                                System.out.println(p);
                                System.out.println(urlStr);
                                if(p.indexOf(urlStr) != -1){
                                    path = p;
                                    break;
                                }
                            }
                            if(path == null){
                                response.output(HttpProtocolUtil.getHttpHeader404());
                            }else{
                                response.outputHtml(path);
                            }
                        } else {
                            httpServlet.service(request, response);
                        }
                    }
                }
            }



/*            if(servletMap.get(request.getUrl()) == null){
                response.outputHtml(request.getUrl());
            }else{
                HttpServlet httpServlet = servletMap.get(request.getUrl());
                httpServlet.service(request, response);
            }*/

            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
